package com.iwomy.secureplat.platform.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.secureplat.common.core.util.R;
import com.iwomy.secureplat.common.log.annotation.SysLog;
import com.iwomy.secureplat.platform.dto.BasicEventTypeDTO;
import com.iwomy.secureplat.platform.entity.BasicEventTypeEntity;
import com.iwomy.secureplat.platform.exception.PlatformException;
import com.iwomy.secureplat.platform.service.BasicEventTypeService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.secureplat.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 事件类型表
 *
 * @author pig
 * @date 2023-11-13 17:05:27
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/basicEventType" )
@Tag(description = "basicEventType" , name = "事件类型表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BasicEventTypeController {

    private final  BasicEventTypeService basicEventTypeService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param basicEventTypeDTO 事件类型表
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('platform_basicEventType_view')" )
    public R getBasicEventTypePage(@ParameterObject Page page, @ParameterObject BasicEventTypeDTO basicEventTypeDTO) {
        return R.ok(basicEventTypeService.getBasicEventTypePage(page, basicEventTypeDTO));
    }

	/**
	 * 分页查询
	 * @param basicEventType 设备类型
	 * @return
	 */
	@Operation(summary = "分页查询" , description = "分页查询" )
	@GetMapping("/list" )
	@PreAuthorize("@pms.hasPermission('platform_basicEventType_view')" )
	public R getBasicEventTypeList( @ParameterObject BasicEventTypeEntity basicEventType) {
		LambdaQueryWrapper<BasicEventTypeEntity> wrapper = Wrappers.lambdaQuery(basicEventType);
		return R.ok(basicEventTypeService.list( wrapper));
	}

    /**
     * 通过id查询事件类型表
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('platform_basicEventType_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(basicEventTypeService.getById(id));
    }

    /**
     * 新增事件类型表
     * @param basicEventType 事件类型表
     * @return R
     */
    @Operation(summary = "新增事件类型表" , description = "新增事件类型表" )
    @SysLog("新增事件类型表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('platform_basicEventType_add')" )
    public R save(@RequestBody BasicEventTypeEntity basicEventType) {
		BasicEventTypeEntity wrapperEntity = new BasicEventTypeEntity();
		wrapperEntity.setTypeName(basicEventType.getTypeName());
		LambdaQueryWrapper<BasicEventTypeEntity> wrapper = Wrappers.lambdaQuery(wrapperEntity);
		BasicEventTypeEntity one = basicEventTypeService.getOne(wrapper);
		if (ObjectUtil.isNotNull(one)) {
			throw new PlatformException("事件类型已存在");
		}
        return R.ok(basicEventTypeService.save(basicEventType));
    }

    /**
     * 修改事件类型表
     * @param basicEventType 事件类型表
     * @return R
     */
    @Operation(summary = "修改事件类型表" , description = "修改事件类型表" )
    @SysLog("修改事件类型表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('platform_basicEventType_edit')" )
    public R updateById(@RequestBody BasicEventTypeEntity basicEventType) {
		BasicEventTypeEntity wrapperEntity = new BasicEventTypeEntity();
		wrapperEntity.setTypeName(basicEventType.getTypeName());
		LambdaQueryWrapper<BasicEventTypeEntity> wrapper = Wrappers.lambdaQuery(wrapperEntity);
		BasicEventTypeEntity one = basicEventTypeService.getOne(wrapper);
		if (ObjectUtil.isNotNull(one)&&!one.getId().equals(basicEventType.getId())){
			throw new PlatformException("事件类型已存在");
		}
        return R.ok(basicEventTypeService.updateById(basicEventType));
    }

    /**
     * 通过id删除事件类型表
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除事件类型表" , description = "通过id删除事件类型表" )
    @SysLog("通过id删除事件类型表" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('platform_basicEventType_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(basicEventTypeService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param basicEventType 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('platform_basicEventType_export')" )
    public List<BasicEventTypeEntity> export(BasicEventTypeEntity basicEventType,Long[] ids) {
        return basicEventTypeService.list(Wrappers.lambdaQuery(basicEventType).in(ArrayUtil.isNotEmpty(ids), BasicEventTypeEntity::getId, ids));
    }
}